package com.yesin.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yesin.geteway.exception.ExceptionEnum;
import com.yesin.geteway.exception.YsException;
import com.yesin.geteway.vo.PageResult;
import com.yesin.item.mapper.BrandMapper;
import com.yesin.item.pojo.Brand;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;
    public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        //分页
        PageHelper.startPage(page,rows);
        /**
         *  where 'name' like "%x%" or letter == 'X'
         *  order by id desc
         */
        //过滤
        Example example = new Example(Brand.class);
        if(StringUtils.isNotBlank(key)){
            //过滤条件
            example.createCriteria().orLike("name","%"+key+"%")
                    .orEqualTo("letter",key.toUpperCase());
        }

        //排序
        if(StringUtils.isNotBlank(sortBy)){
            String orderByCaulse = sortBy + (desc ? " DESC":" ASC");
            example.setOrderByClause(orderByCaulse);
        }
        //查询
        List<Brand> list = brandMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            throw new YsException(ExceptionEnum.BRAND_CANNOT_BE_NULL);
        }
        //解析分页结果
        PageInfo<Brand> info = new PageInfo<>(list);
        return new PageResult<>(info.getTotal(), list);
    }

    /**
     * 新增品牌
     * @param brand
     * @param cids
     */
    @Transactional
    public void saveBrand(Brand brand, List<Long> cids) {
        //新增品牌
        brand.setId(null);
        int count = brandMapper.insert(brand);
        if(count != 1){
            throw new YsException(ExceptionEnum.GATEGORY_NOT_FOND);
        }
        //新增中间表
        for(Long cid : cids){
            brandMapper.insertCategoryBrand(cid,brand.getId());
            if(count != 1){
                throw new YsException(ExceptionEnum.BRAND_SAVE_ERROR);
            }
        }
    }

    /**
     * 修改品牌
     * @param brand
     * @param cids
     */
    @Transactional
    public void updateBrand(Brand brand, List<Long> cids) {
        int count = brandMapper.updateByPrimaryKey(brand);
        if(count != 1){
            throw new YsException(ExceptionEnum.GATEGORY_NOT_FOND);
        }
        //修改中间表
        for(Long cid : cids){
            brandMapper.updateCategoryBrand(cid,brand.getId());
            if(count != 1){
                throw new YsException(ExceptionEnum.BRAND_UPDATE_ERROR);
            }
        }
    }

    public List<Brand> queryBrandsByCid(Long cid) {
        return this.brandMapper.selectBrandByCid(cid);
    }


    /**
     * 删除品牌
     * @param id
     */
    public void deleteBrand(Long id) {
        Brand brand = new Brand();
        brand.setId(id);
        int count = this.brandMapper.delete(brand);
        if (count != 1){
            throw new YsException(ExceptionEnum.BRAND_DELETE_ERROR);
        }
    }

    public Brand queryBrandByBid(Long id) {
        Brand brand = this.brandMapper.selectByPrimaryKey(id);
        if (brand == null){
            throw new YsException(ExceptionEnum.BRAND_CANNOT_BE_NULL);
        }
        return brand;
    }
}
